package com.dayouzi.esaydata.service.impl;

import com.dayouzi.esaydata.mapper.UserInfoMapper;
import com.dayouzi.esaydata.mapper.UserStatusMapper;
import com.dayouzi.esaydata.pojo.page.PageBean;
import com.dayouzi.esaydata.pojo.query.UserInfoQuery;
import com.dayouzi.esaydata.pojo.vo.UserInfoDTO;
import com.dayouzi.esaydata.pojo.vo.UserInfoVO;
import com.dayouzi.esaydata.service.UserInfoService;
import com.dayouzi.esaydata.utils.MD5;
import com.dayouzi.esaydata.utils.RandomUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DaYouZi
 * @since 2023-03-06
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserStatusMapper userStatusMapper;

    @Override
    public UserInfoVO getInfoById(Integer id) {
        return userInfoMapper.getInfoById(id);
    }

    @Override
    public Boolean removeInfoById(Integer id) {
        return userInfoMapper.removeInfoById(id);
    }

    @Override
    public Boolean resumeById(Integer id) {
        return userInfoMapper.resumeById(id);
    }

    @Transactional
    @Override
    public Boolean foreverRemove(Integer id) {
        Boolean result1 = userInfoMapper.foreverRemove(id);
        Boolean result2 = userStatusMapper.foreverRemove(id);
        return result1 && result2;
    }

    @Transactional
    @Override
    public Boolean updateInfoById(UserInfoDTO userInfoDTO) {
        Boolean result1 = userInfoMapper.updateInfoById(userInfoDTO);
        Boolean result2 = userStatusMapper.updateStatusByUserId(userInfoDTO);
        return result1 && result2;
    }

    @Transactional
    @Override
    public Boolean insertUserInfo(UserInfoDTO userInfoDTO) {
        LocalDateTime now = LocalDateTime.now();
        userInfoDTO.setGmtCreate(now);
        userInfoDTO.setGmtModified(now);
        Boolean result1 = userInfoMapper.insertUserInfo(userInfoDTO);
        Boolean result2 = userStatusMapper.insertUserStatus(userInfoDTO);
        return result1 && result2;
    }

    @Override
    public PageBean<UserInfoVO> listPage(Integer page, Integer limit, UserInfoQuery userInfoQuery) {
        PageHelper.startPage(page, limit);
        List<UserInfoVO> list = userInfoMapper.listPage(userInfoQuery);
        return new PageBean<>(list);
    }

    @Override
    public String initPassword(String userAccount) {
        //生成4位随机数
        String fourBitRandom = RandomUtils.getFourBitRandom();
        String password = userAccount + fourBitRandom;
        String result = "";
        try {
            result = MD5.md5(password);
            log.info("随机密码生成成功!");
        } catch (Exception e) {
            log.error("MD5加密失败!");
            e.printStackTrace();
        }
        return result;
    }
}
